package com.irdstudio.bhcredit.core.batch.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.irdstudio.bhcredit.core.batch.BatchConstant;
import com.irdstudio.bhcredit.core.util.date.CurrentDateUtil;
import com.irdstudio.bhcredit.core.util.date.DateCalculate;

/**
 * 数据表操作类-[表名: 01.批次实例信息(bat_inst_batch)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-05-10
 */
public class BatInstBatchDao {

	/* 连接对象 */
	Connection conn = null;	

	public BatInstBatchDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public BatInstBatch queryWithKeys(String batchId)
			throws SQLException {
		BatInstBatch dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM bat_inst_batch WHERE batch_id=?");
			ps.setString(1,batchId);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new BatInstBatch();
				dc.setBatchId(rs.getString("batch_id"));
				dc.setBatchOrder(rs.getInt("batch_order"));
				dc.setBatchName(rs.getString("batch_name"));
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setBatchState(rs.getString("batch_state"));
				dc.setBatchInterveneState(rs.getString("batch_intervene_state"));
				dc.setStageId(rs.getString("stage_id"));
				dc.setStageName(rs.getString("stage_name"));
				dc.setBatchDate(rs.getString("batch_date"));
				dc.setStartTime(rs.getString("start_time"));
				dc.setEndTime(rs.getString("end_time"));
				dc.setCostTime(rs.getBigDecimal("cost_time"));
				dc.setNeedRunCount(rs.getInt("need_run_count"));
				dc.setExistRunCount(rs.getInt("exist_run_count"));
				dc.setNotRunCount(rs.getInt("not_run_count"));
				dc.setSucceedRunCount(rs.getInt("succeed_run_count"));
				dc.setFaildRunCount(rs.getInt("faild_run_count"));
				dc.setWarnRunCount(rs.getInt("warn_run_count"));
				dc.setSkipRunCount(rs.getInt("skip_run_count"));
				dc.setBatchSerialNo(rs.getString("batch_serial_no"));
				dc.setRemark(rs.getString("remark"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryBatInstBatchWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
	public List<BatInstBatch> queryWithCond(String szCondition,String szOrderBy)
			throws SQLException {
		List<BatInstBatch> dcList = new ArrayList<BatInstBatch>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM bat_inst_batch "
					+ szCondition + " " + szOrderBy);
			rs = ps.executeQuery();
			while (rs.next()) {
				BatInstBatch dc = new BatInstBatch();
				dc.setBatchId(rs.getString("batch_id"));
				dc.setBatchOrder(rs.getInt("batch_order"));
				dc.setBatchName(rs.getString("batch_name"));
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setBatchState(rs.getString("batch_state"));
				dc.setBatchInterveneState(rs.getString("batch_intervene_state"));
				dc.setStageId(rs.getString("stage_id"));
				dc.setStageName(rs.getString("stage_name"));
				dc.setBatchDate(rs.getString("batch_date"));
				dc.setStartTime(rs.getString("start_time"));
				dc.setEndTime(rs.getString("end_time"));
				dc.setCostTime(rs.getBigDecimal("cost_time"));
				dc.setNeedRunCount(rs.getInt("need_run_count"));
				dc.setExistRunCount(rs.getInt("exist_run_count"));
				dc.setNotRunCount(rs.getInt("not_run_count"));
				dc.setSucceedRunCount(rs.getInt("succeed_run_count"));
				dc.setFaildRunCount(rs.getInt("faild_run_count"));
				dc.setWarnRunCount(rs.getInt("warn_run_count"));
				dc.setSkipRunCount(rs.getInt("skip_run_count"));
				dc.setBatchSerialNo(rs.getString("batch_serial_no"));
				dc.setRemark(rs.getString("remark"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryBatInstBatchWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 更新批次实例阶段
	 * @param batchInst
	 * @throws SQLException 
	 */
	public void updateStage(BatInstBatch batchInst) throws SQLException {
		PreparedStatement ps = null;
		try {
			BatBatchStageConfigDao stageDao = new BatBatchStageConfigDao(
					this.conn);
			BatBatchStageConfig stageInfo = stageDao.queryWithKeys(batchInst
					.getStageId(), batchInst.getBatchId());
			ps = conn
					.prepareStatement("update bat_inst_batch set batch_state=?,stage_id=?,stage_name=? where batch_id=?");
			ps.setString(1, String.valueOf(BatchConstant.BATCH_STATE_RUNNING));
			ps.setString(2, batchInst.getStageId());
			ps.setString(3, stageInfo.getStageName());
			ps.setString(4, batchInst.getBatchId());
			ps.execute();
		} catch (SQLException e) {
			throw e;
		} finally {
			close(null, null, ps);
		}
	}
	
	/**
	 * 将批次更改为结束
	 * @param batchInst
	 * @throws SQLException
	 */
	public void updateBatchToEnd(BatInstBatch batchInst) throws SQLException {
		PreparedStatement ps = null;
		try {
			ps = conn
					.prepareStatement("update bat_inst_batch set batch_state=?,end_time=?,cost_time=?,remark=? where batch_id=?");
			String endTime = CurrentDateUtil.getTodayDateEx2();
			ps.setString(1, batchInst.getBatchState());
			ps.setString(2, endTime);
			ps.setBigDecimal(3, DateCalculate.computeIntervalMills(batchInst
					.getStartTime(), endTime));
			ps.setString(4, batchInst.getRemark());
			ps.setString(5, batchInst.getBatchId());
			ps.execute();
		} catch (SQLException e) {
			throw e;
		} finally {
			close(null, null, ps);
		}		
	}
}
